package com.calm.cdemo.activitytransition;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.calm.cdemo.R;
import com.calm.cdemo.base.BaseActivity;
import com.calm.clibrary.utils.Constant;

/**
 * Create by Calm
 * 2020/1/9 11:21
 * 转场动画弹出的页面
 */
public class TranisitionActivity extends BaseActivity {
    private int animIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        animIndex = getIntent().getIntExtra(Constant.Params1,-1);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            if(animIndex == 6){
                getWindow().setEnterTransition(new Explode().setDuration(1000));
                getWindow().setReturnTransition(new Explode().setDuration(1000));
            }else if(animIndex == 7){
                getWindow().setEnterTransition(new Slide(Gravity.LEFT).setDuration(1000));
                getWindow().setReturnTransition(new Slide(Gravity.LEFT).setDuration(1000));
            }else if(animIndex == 8){
                getWindow().setEnterTransition(new Fade(Fade.IN).setDuration(1000));
                getWindow().setReturnTransition(new Fade(Fade.OUT).setDuration(1000));
            }
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        TextView tvContent = findViewById(R.id.transitionactivity_tv_content);
        String content = getIntent().getStringExtra(Constant.Params0);
        if(!TextUtils.isEmpty(content)){
            tvContent.setText(content);
        }
        setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFinish();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tranisitionactivity;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                onFinish();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private void onFinish(){
        ActivityCompat.finishAfterTransition(this);
        switch (animIndex){
            case 0:
                overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                break;
            case 1:
                overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
                break;
            case 2:
                overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 3:
                overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
                break;
            case 4:
                overridePendingTransition(R.anim.slide_in_top,R.anim.slide_out_bottom);
                break;
            case 5:
                overridePendingTransition(R.anim.slide_in_bottom,R.anim.slide_out_top);
                break;
        }
    }
}
